﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using System.IO;

namespace Gollie.Menu
{
	public enum MenuDirection
	{
		Horizontal,
		Vertical
	}

	public class MainMenu : MenuItem
	{
		string type;
		string css;
		string submenuCss;
		string separatorCss;
		MenuDirection direction;
		int menuLevel = 0;
		string html;

		public MainMenu(string type, MenuDirection direction, string css, string submenuCss, string separatorCss)
			: base()
		{
			this.type = type;
			this.direction = direction;
			this.css = css;
			this.submenuCss = submenuCss;
			this.separatorCss = separatorCss;
		}

		internal void RenderHtml()
		{
			this.RenderMenu();
		}

		private void RenderMenu()
		{
			StringBuilder htmlString = new StringBuilder(1024);
			StringWriter stringWriter = new StringWriter(htmlString);
			HtmlTextWriter writer = new HtmlTextWriter(stringWriter);

			this.menuLevel = 0;

			writer.WriteBeginTag("div");
			if (this.Direction == MenuDirection.Horizontal)
				this.RenderCSS("menu", writer);
			else
				this.RenderCSS("menudown", writer);

			writer.Write(HtmlTextWriter.TagRightChar);

			this.RenderItems(this, writer, false);

			writer.WriteEndTag("div");

			html = htmlString.ToString();
		}

		private void RenderItems(MenuItem menu, HtmlTextWriter writer, bool sub)
		{
			this.menuLevel++;

			writer.WriteBeginTag("ul");

			writer.Write(HtmlTextWriter.TagRightChar);

			foreach (MenuItem item in menu)
			{
				this.RenderItem(item, writer);
			}

			writer.WriteEndTag("ul");

			this.menuLevel--;
		}

		private void RenderItem(Gollie.Menu.MenuItem item, HtmlTextWriter writer)
		{
			writer.WriteBeginTag("li");

			if (item.IsSeparator)
			{
				//if (this.menuLevel == 1) //must be a vertical menu
				this.RenderCSS("separator", writer);

				writer.Write(HtmlTextWriter.TagRightChar);

				if (this.menuLevel > 1)
				{
					writer.WriteBeginTag("div");
					this.RenderCSS("separator", writer);
					writer.Write(HtmlTextWriter.TagRightChar);
					writer.WriteEndTag("div");
				}
			}
			else
			{
				if (string.IsNullOrEmpty(item.ItemCss) == false)
				{
					this.RenderCSS(item.ItemCss, writer);
				}

				writer.Write(HtmlTextWriter.TagRightChar);

				//string itemText = (this.UserInfo == null ? item.Text : item.Text.Replace("{username}", this.UserInfo.Gebruikersnaam));
				string itemText = item.Text;

				if (string.IsNullOrEmpty(item.Href) && string.IsNullOrEmpty(item.OnClick) && item.Count == 0)
				{
					writer.WriteBeginTag("span");
					writer.Write(HtmlTextWriter.TagRightChar);
					writer.Write(itemText);
					writer.WriteEndTag("span");
				}
				else
				{
					writer.WriteBeginTag("a");
					if (item.Count == 0)
					{
						if (string.IsNullOrEmpty(item.Href) == false)
							writer.WriteAttribute("href", item.Href);
						else
							writer.WriteAttribute("href", "#");

						if (string.IsNullOrEmpty(item.Target) == false)
							writer.WriteAttribute("target", item.Target);

						if (string.IsNullOrEmpty(item.OnClick) == false)
							writer.WriteAttribute("onclick", item.OnClick);
					}
					else
						writer.WriteAttribute("href", "#");

					writer.Write(HtmlTextWriter.TagRightChar);

					writer.Write(itemText);
					if (item.Count > 0)
						writer.Write("&nbsp;»");

					writer.WriteEndTag("a");

					if (item.Count > 0)
					{
						this.RenderItems(item, writer, true);
					}
				}
			}

			writer.WriteEndTag("li");
		}

		private void RenderCSS(string className, HtmlTextWriter writer)
		{
			if (string.IsNullOrEmpty(className) == false)
				writer.WriteAttribute("class", className);
		}



		public string Css
		{
			get { return this.css; }
		}

		public string SubmenuCss
		{
			get { return this.submenuCss; }
		}

		public string SeparatorCss
		{
			get { return this.separatorCss; }
		}

		public string Type
		{
			get { return this.type; }
		}

		public MenuDirection Direction
		{
			get { return this.direction; }
		}

		public string Html
		{
			get { return this.html; }
		}
	}
}